package com.engineering.elink.hessian.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.dao.CompanyDynamicMapper;
import com.engineering.elink.domain.CompanyDynamic;
import com.engineering.elink.hessian.CompanyDynamicHessianService;

@Service
public class CompanyDynamicHessianServiceImpl implements CompanyDynamicHessianService{

	@Autowired
	private CompanyDynamicMapper companyDynamicMapper;
	
	@Override
	public  boolean addCompanyDynamic(int cdType, String cdContent,
			String userId, String fkId) {
		CompanyDynamic companyDynamicInSql = companyDynamicMapper.selectByFkIdAndCdType(fkId, cdType);
		if (companyDynamicInSql == null) {
			CompanyDynamic cd = new CompanyDynamic();
			cd.setCreateTime(new Date());
			cd.setFkId(fkId);
			cd.setCdContent(cdContent);
			cd.setUserId(userId);
			cd.setCdType(cdType);
			companyDynamicMapper.insertSelective(cd);
			return true;
		}
		return false;
	}

	@Override
	public boolean removeCompanyDynamic(int cdType, String fkId) {
		CompanyDynamic companyDynamic = companyDynamicMapper.selectByFkIdAndCdType(fkId, cdType);
		if (companyDynamic != null) {
			companyDynamicMapper.deleteByPrimaryKey(companyDynamic.getCdId());
			return true;
		}
		return false;
	}

	@Override
	public boolean modifyCompanyDynamic(int cdType, String cdContent,
			String fkId) {
		CompanyDynamic companyDynamicInSql = companyDynamicMapper.selectByFkIdAndCdType(fkId, cdType);
		if (companyDynamicInSql != null) {
			CompanyDynamic companyDynamic = new CompanyDynamic();
			companyDynamic.setCdContent(cdContent);
			companyDynamic.setCdType(cdType);
			companyDynamic.setFkId(fkId);
			companyDynamic.setCreateTime(new Date());
			companyDynamic.setUserId(companyDynamicInSql.getUserId());
			companyDynamic.setCdId(companyDynamicInSql.getCdId());
			companyDynamicMapper.updateByPrimaryKeySelective(companyDynamic);
			return true;
		}
		return false;
	}

}
